$(function(){
	if($("#ty").val()==1){
		init(0);
		$("#allUser").change(function(){
			userSelect(0);
		});
		$("#ser").click(function(){
			findLike(0);
		});
		$("#hea a").eq(0).css({"color":"#FF6000","border-bottom":"2px solid #FF6000"});
	}
})
function init(i){
	pageIndex = i;
	pageCount = -1;
	$.ajax({
		"url":"allOrder",
		"type":"post",
		async:false,
		"data":"opr=init",
		"dataType":"json",
		"success":function(data){
			$("#list").empty();
			var str = "";
			var num = 0;
			for(var o in data){
				if(pageIndex == num){
					var sz = o.split("|");
					str += "<li><p class='zhu'><span>用户名："+sz[0].split(":")[1]+"</span><span>订单号："+sz[2].split(":")[1]+"</span>"+
    					"<span>地址："+sz[1].split(":")[1]+"</span></p>";
					for(i=0;i<data[o].length;i++){
						str += "<p class='p'><span class='in'><img src='images/"+data[o][i].fileName+"'/><span class='name'>"+data[o][i].name+"</span></span>" +
								"<p class='gs'>";
						if(data[o][i].specification!=undefined && data[o][i].specification!=""){
							str += "<span>规格："+data[o][i].specification+"</span>";
						}
						if(data[o][i].specification!=undefined && data[o][i].specification!="" && data[o][i].color!=undefined && data[o][i].color!=""){
							str += "<br/>";
						}
						if(data[o][i].color!=undefined && data[o][i].color!=""){
							str += "<span>颜色："+data[o][i].color+"</span>";
						}
						str += "</p><span class='price'>￥"+data[o][i].price+"</span>"+
							"<span class='num'>"+data[o][i].quantity+"</span><span class='cost'>￥"+data[o][i].cost+"</span><span class='sta'>"+data[o][i].status+"</span></p>";
					}
					str += "</li>";
				}
				num++;
				pageCount++;
			}
			if(num == 0){
				$("#list").append("<table id='noo'><tr><td>没有相关的订单</td></tr></table>");
			}
			$("#list").append(str);
		}
	})
	$.ajax({
		"url":"allOrder",
		"type":"post",
		async:false,
		"data":"opr=initUser",
		"dataType":"json",
		"success":function(data){
			$("#allUser").empty();
			$("#allUser").append("<option value=''>所有用户</option>");
			for(i=0;i<data.length;i++){
				$("#allUser").append("<option value="+data[i].loginName+">"+data[i].loginName+"</option>");
			}
		}
	})
	paging();
	tq();
}
var pageIndex = 0;
var pageCount = 0;
var type = 0;
function userSelect(i){
	type = 1;
	pageIndex = i;
	pageCount = -1;
	var loginName = $("#allUser").val();
	$.ajax({
		"url":"allOrder",
		"type":"post",
		async:false,
		"data":"opr=userSelect&loginName="+loginName+"&status="+sts,
		"dataType":"json",
		"success":function(data){
			$("#list").empty();
			var str = "";
			var num = 0;
			for(var o in data){
				if(pageIndex == num){
					var sz = o.split("|");
					str += "<li><p class='zhu'><span>用户名："+sz[0].split(":")[1]+"</span><span>订单号："+sz[2].split(":")[1]+"</span>"+
    					"<span>地址："+sz[1].split(":")[1]+"</span></p>";
					for(i=0;i<data[o].length;i++){
						str += "<p class='p'><span class='in'><img src='images/"+data[o][i].fileName+"'/><span class='name'>"+data[o][i].name+"</span></span>" +
								"<p class='gs'>";
						if(data[o][i].specification!=undefined && data[o][i].specification!=""){
							str += "<span>规格："+data[o][i].specification+"</span>";
						}
						if(data[o][i].specification!=undefined && data[o][i].specification!="" && data[o][i].color!=undefined && data[o][i].color!=""){
							str += "<br/>";
						}
						if(data[o][i].color!=undefined && data[o][i].color!=""){
							str += "<span>颜色："+data[o][i].color+"</span>";
						}
						str += "</p><span class='price'>￥"+data[o][i].price+"</span>"+
							"<span class='num'>"+data[o][i].quantity+"</span><span class='cost'>￥"+data[o][i].cost+"</span><span class='sta'>"+data[o][i].status+"</span></p>";
					}
					str += "</li>";
				}
				num++;
				pageCount++;
			}
			if(num == 0){
				$("#list").append("<table id='noo'><tr><td>没有相关的订单</td></tr></table>");
			}
			$("#list").append(str);
		}
	})
	paging();
	tq();
}
function findLike(i){
	type = 2;
	pageIndex = i;
	pageCount = -1;
	var sn = $("#con").val();
	var loginName = $("#allUser").val();
	$.ajax({
		"url":"allOrder",
		"type":"post",
		async:false,
		"data":"opr=findLike&sn="+sn+"&loginName="+loginName+"&status="+sts,
		"dataType":"json",
		"success":function(data){
			$("#list").empty();
			var str = "";
			var num = 0;
			for(var o in data){
				if(pageIndex == num){
					var sz = o.split("|");
					str += "<li><p class='zhu'><span>用户名："+sz[0].split(":")[1]+"</span><span>订单号："+sz[2].split(":")[1]+"</span>"+
    					"<span>地址："+sz[1].split(":")[1]+"</span></p>";
					for(i=0;i<data[o].length;i++){
						str += "<p class='p'><span class='in'><img src='images/"+data[o][i].fileName+"'/><span class='name'>"+data[o][i].name+"</span></span>" +
								"<p class='gs'>";
						if(data[o][i].specification!=undefined && data[o][i].specification!=""){
							str += "<span>规格："+data[o][i].specification+"</span>";
						}
						if(data[o][i].specification!=undefined && data[o][i].specification!="" && data[o][i].color!=undefined && data[o][i].color!=""){
							str += "<br/>";
						}
						if(data[o][i].color!=undefined && data[o][i].color!=""){
							str += "<span>颜色："+data[o][i].color+"</span>";
						}
						str += "</p><span class='price'>￥"+data[o][i].price+"</span>"+
							"<span class='num'>"+data[o][i].quantity+"</span><span class='cost'>￥"+data[o][i].cost+"</span><span class='sta'>"+data[o][i].status+"</span></p>";
					}
					str += "</li>";
				}
				num++;
				pageCount++;
			}
			if(num == 0){
				$("#list").append("<table id='noo'><tr><td>没有相关的订单</td></tr></table>");
			}
			$("#list").append(str);
		}
	})
	paging();
	tq();
}
var sts = 0;
function findTypeStatus(status,i){
	type = 3;
	pageIndex = i;
	pageCount = -1;
	sts = status;
	$("#kz").removeClass("a");
	$("#hea a").css({"color":"#555","border":"none"});
	$("#hea a").eq(sts).css({"color":"#FF6000","border-bottom":"2px solid #FF6000"});
	$.ajax({
		"url":"allOrder",
		"type":"post",
		"data":"opr=findTypeStatus&status="+status,
		async:false,
		"dataType":"json",
		"success":function(data){
			$("#list").empty();
			var str = "";
			var num = 0;
			for(var o in data){
				if(pageIndex == num){
					var sz = o.split("|");
					str += "<li><p class='zhu'><span>用户名："+sz[0].split(":")[1]+"</span><span>订单号："+sz[2].split(":")[1]+"</span>"+
    					"<span>地址："+sz[1].split(":")[1]+"</span></p>";
					for(i=0;i<data[o].length;i++){
						str += "<p class='p'><span class='in'><img src='images/"+data[o][i].fileName+"'/><span class='name'>"+data[o][i].name+"</span></span>" +
								"<p class='gs'>";
						if(data[o][i].specification!=undefined && data[o][i].specification!=""){
							str += "<span>规格："+data[o][i].specification+"</span>";
						}
						if(data[o][i].specification!=undefined && data[o][i].specification!="" && data[o][i].color!=undefined && data[o][i].color!=""){
							str += "<br/>";
						}
						if(data[o][i].color!=undefined && data[o][i].color!=""){
							str += "<span>颜色："+data[o][i].color+"</span>";
						}
						str += "</p><span class='price'>￥"+data[o][i].price+"</span>"+
							"<span class='num'>"+data[o][i].quantity+"</span><span class='cost'>￥"+data[o][i].cost+"</span><span class='sta'>"+data[o][i].status+"</span></p>";
					}
					str += "</li>";
				}
				num++;
				pageCount++;
			}
			if(num == 0){
				$("#list").append("<table id='noo'><tr><td>没有相关的订单</td></tr></table>");
			}
			$("#list").append(str);
		}
	})
	paging();
	tq();
}
function tq(){
	if($("#bt").offset().top<780){
		$("#kz").addClass("a");
	}
}
function paging(){
	var lj = "void(0)";
	switch(type){
		case 1:
			lj = "userSelect(";
		break;
		case 2:
			lj = "findLike(";
		break;
		case 3:
			lj = "findTypeStatus("+sts+",";
		break;
		default:
			lj = "init(";
		break;
	}
	$("#paging").empty();
	if(pageCount == -1){
		
	} else {
		$("#paging").append("<a href='javascript:"+lj+"0)' class='p_pre'>首页</a>");
		if (pageIndex == 0 && pageCount == 0){
			$("#paging").append(pageNum());
		} else if (pageIndex == 0){
			$("#paging").append(pageNum());
			$("#paging").append("<a href='javascript:"+lj+"pageIndex+1)' class='p_pre'>下一页</a>");
		} else if (pageIndex == pageCount){
			$("#paging").append("<a href='javascript:"+lj+"pageIndex-1)' class='p_pre'>上一页</a>");
			$("#paging").append(pageNum());
		} else {
			$("#paging").append("<a href='javascript:"+lj+"pageIndex-1)' class='p_pre'>上一页</a>");
			$("#paging").append(pageNum());
			$("#paging").append("<a href='javascript:"+lj+"pageIndex+1)' class='p_pre'>下一页</a>");
		}
		$("#paging").append("<a href='javascript:"+lj+"pageCount)' class='p_pre'>尾页</a>");
		bs();
	}
}
function pageNum(){
	var lj = "void(0)";
	switch(type){
		case 1:
			lj = "userSelect(";
		break;
		case 2:
			lj = "findLike(";
		break;
		case 3:
			lj = "findTypeStatus("+sts+",";
		break;
		default:
			lj = "init(";
		break;
	}
	var str = "";
	if(pageCount < 6){
		for(i=0;i<=pageCount;i++){
			str += "<a href='javascript:"+lj+i+")'>"+(i+1)+"</a>";
		}
	} else {
		for(i=0;i<=pageCount;i++){
			if(pageIndex == 1){
				if(i>3 && i< pageCount-1){
					continue;
				}
				if(i == pageCount-1){
					str += "...";
				}
				str += "<a href='javascript:"+lj+i+")'>"+(i+1)+"</a>";
			} else if (pageIndex <= pageCount - 3) {
				if(pageIndex >= (i-1) && pageIndex<=(i+1)){
					str += "<a href='javascript:"+lj+i+")'>"+(i+1)+"</a>";
				}
				if(i == pageCount -1){
					str += "...";
				}
				if(i >= pageCount-1){
					str += "<a href='javascript:"+lj+i+")'>"+(i+1)+"</a>";
				}
			} else if (pageIndex == pageCount - 2){
				if(i > pageIndex-3 && i <= pageIndex){
					str += "<a href='javascript:"+lj+i+")'>"+(i+1)+"</a>";
				}
				if(i == pageIndex){
					str += "...";
				}
				if(i >pageCount -2){
					str += "<a href='javascript:"+lj+i+")'>"+(i+1)+"</a>";
				}
			} else if (pageIndex >= pageCount - 3){
				if(i<=3){
					str += "<a href='javascript:"+lj+i+")'>"+(i+1)+"</a>";
				}
				if(i==3){
					str += "...";
				}
				if(i > pageCount - 2){
					str += "<a href='javascript:"+lj+i+")'>"+(i+1)+"</a>";
				}
			}
		}
	}
	return str;
}
function bs(){
	$("#paging a").each(function(){
		if((Number(this.text)-1)==pageIndex){
			$(this).css({"background-color":"#FF4E00","color":"#fff"});
		}
	})
}